package com.benshou.bcss.common.rest;

import com.benshou.bcss.common.j17common.Pagination;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 分页请求
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PagingRequest implements Pagination {

    /**
     * 取消分页时，pageSize 的值
     */
    private static final long PAGE_SIZE_PAGINATION_DISABLED = -1;

    /**
     * 取消分页时，PagingRequest 的实例
     */
    public static final PagingRequest DISABLED =  new _PaginationDisabledPagingRequest();

    /**
     * 页码
     */
    @NotNull
    private Long pageNo;

    /**
     * 一页包含的记录数
     */
    @NotNull
    private Long pageSize;

    @Override
    public long pageNo() {
        return this.pageNo;
    }

    @Override
    public long pageSize() {
        return this.pageSize;
    }

    @Override
    public boolean paginationDisabled() {
        return this.pageSize != null && this.pageSize == PAGE_SIZE_PAGINATION_DISABLED;
    }

    /**
     * 取消分页的分页请求
     */
    private static class _PaginationDisabledPagingRequest extends PagingRequest {

        private _PaginationDisabledPagingRequest() {
            super(1L, PAGE_SIZE_PAGINATION_DISABLED);
        }

        @Override
        public PagingRequest setPageNo(Long pageNo) {
            throw new UnsupportedOperationException();
        }

        @Override
        public PagingRequest setPageSize(Long pageSize) {
            throw new UnsupportedOperationException();
        }
    }
}
